package birthday.syj.com.manage.common.activity;

/**
 * Created by 18159 on 2017/12/24.
 */

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.joanzapata.android.BaseAdapterHelper;
import com.joanzapata.android.QuickAdapter;

import org.litepal.crud.DataSupport;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import birthday.syj.com.manage.DB.GroupBean;
import birthday.syj.com.manage.DB.MyFriends;
import birthday.syj.com.manage.R;


public class PeopleListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{

    Toolbar toolbar;

    ListView mListView;

    private QuickAdapter<MyFriends> quickAdapter;

    private GroupBean groupBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group);
        mListView= (ListView) findViewById(R.id.listview);
        groupBean= (GroupBean) getIntent().getSerializableExtra("bean");
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        //为toolbar设置支持
        setSupportActionBar(toolbar);
        toolbar.setTitle(groupBean.getGroupname());
        mListView.setOnItemClickListener(this);
        initAdapter();
        initData();
    }

    private void initData() {
        List<MyFriends> list = DataSupport.where("groupid=?",groupBean.getId()+"").find(MyFriends.class);
        quickAdapter.clear();
        quickAdapter.addAll(list);
        quickAdapter.notifyDataSetChanged();
    }

    private void initAdapter() {

        quickAdapter = new QuickAdapter<MyFriends>(this, R.layout.list_item) {

            @Override
            protected void convert(BaseAdapterHelper baseAdapterHelper, final MyFriends myFriends) {


                baseAdapterHelper.setText(R.id.date,myFriends.getDate());
                baseAdapterHelper.setText(R.id.name,myFriends.getName());

                String daysDivider=getBirthday(myFriends.getBirthYear()+"-"+myFriends.getBirthMonth()+"-"+myFriends.getBirthDay());
                baseAdapterHelper.setText(R.id.birthDivider,daysDivider+"天"+"");

            }
        };

        quickAdapter.clear();
        mListView.setAdapter(quickAdapter);

    }

    public String  getBirthday(String getBirthDate) {
        String birthday = getBirthDate;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        int yearNow = cal.get(Calendar.YEAR);// 获得当前年份
        try {
            Log.i("Birthday_time", formatter.parse(birthday) + "");
            cal.setTime(formatter.parse(birthday));
        } catch (Exception e) {
            e.printStackTrace();
        }
        int birthyear = cal.get(Calendar.YEAR);
        Log.i("Birthday_year", birthyear + "");
        while (birthyear < yearNow) {
            cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + 1);
            birthyear = cal.get(Calendar.YEAR);
            Log.i("Birthday_year++", birthyear + "");
        }
        Date ed = new Date();
        Log.i("Date_ed", ed + "");
        Date sd = cal.getTime();
        Log.i("Date_sd", ed + "");
        String days;
        if ((ed.getTime() - sd.getTime()) / (3600 * 24 * 1000) < 0) {//>
            days = String.valueOf(-((ed.getTime() - sd.getTime()) / (3600 * 24 * 1000)) + 1);
            return days;
        } else {
            cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + 1);
            sd = cal.getTime();
            days = String.valueOf(-((ed.getTime() - sd.getTime()) / (3600 * 24 * 1000)) + 1);
            return days;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        MyFriends myFriends = quickAdapter.getItem(position);

        Intent intent = new Intent(PeopleListActivity.this, FriendsInfo.class);
        intent.putExtra("Content", myFriends);
        intent.putExtra("id",id);
        intent.putExtra("ActivityName", "1");
        startActivity(intent);
    }


}

